package com.hzp.lolnews.utils;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import com.hzp.lolnews.ui.discovery.view.DiscoveryFragment;
import com.hzp.lolnews.ui.friends.view.FriendsFragment;
import com.hzp.lolnews.ui.me.view.MeFragment;
import com.hzp.lolnews.ui.news.view.NewsFragment;
import com.hzp.lolnews.ui.novel.view.NovelFragment;

/**
 * Created by Administrator on 2016/10/11 0011.
 */
public class FragmentFactory {

    private static final int FRAGMENT_TUI_JIAN = 0;
    private static final int FRAGMENT_PROGRAM = 1;
    private static final int FRAGMENT_LIVE = 2;
    private static final int FRAGMENT_MINE = 3;
    private static final int FRAGMENT_PRO = 4;
    //SparseArray:key是整形的key,根据整形的key来获取一个对象,效率比较高.
    private static SparseArray<Fragment> fragments = new SparseArray<>();

    //创建一个工厂方法,用来创建一个Fragment对象
    public static Fragment createFragment(int index) {
        //从集合中获取
        Fragment fragment = fragments.get(index);
        if (fragment == null) {
            switch (index) {
                case FRAGMENT_TUI_JIAN:
                    fragment = new NewsFragment();
                    break;
                case FRAGMENT_PROGRAM:
                    fragment = new FriendsFragment();
                    break;
                case FRAGMENT_LIVE:
                    fragment = new DiscoveryFragment();
                    break;
                case FRAGMENT_MINE:
                    fragment = new NovelFragment();
                    break;
                case FRAGMENT_PRO:
                    fragment = new MeFragment();
            }
            //存到集合中
            fragments.put(index, fragment);
        }

        return fragment;
    }
}
